/*
* This file is define some function for the program ;
* All are 
*/

#include "l2tp.h"

char * trim(char src[])
{
	int i = 0;
	char *begin = src;
	
	//Add src judged ,if it's NULL , return NULL;
	if(src == NULL)
		return NULL ;
		
	while(src[i] != '\0'){
		if(src[i] != ' '){
			  break;
		}else{
			  begin++;
		}
	   i++;
	}
	for(i = strlen(src)-1; i >= 0;  i--)
	{
		if(src[i] != ' ') {
			  break;
		} else {
			  src[i] = '\0';
		}
	}

	return begin;
}

/*
*	Mypopen function ,it returns the error info
*	when you excute the cmd .
*
*	The type is char ,'r' or 'w';
*/

FILE *mypopen(char *cmd,char type)
{
    int pipefd[2];           //管道描述符
    int pid_t;               //进程描述符
    
    printf("cmd:%s\n", cmd);
    
    if(type !='r' && type != 'w')
    {
        printf("myopen() flag error/n");
        return NULL;
    }
    
    if(pipe(pipefd)<0)        //建立管道
    {
        printf("myopen() pipe create error/n");
        return NULL;
    }
    
    pid_t=fork();             //建立子进程
    
    if(pid_t < 0)
        return NULL;
    
    if(0 == pid_t)            //子进程中......
    {
        if(type == 'r')
        {
            close(pipefd[0]);               //此函数返回文件指针是用于读，因此，在子进程中应该将结果写入管道，这样父进程返回的文件指针才能读，所以这里将读端关闭
            dup2(pipefd[1], STDERR_FILENO);  //exec函数的执行结果将会通过标准输出写到控制台上，但这里我们不需要在控制台输出，而是需要将结果返回，因此将标准输出重定向到管道写端
            close(pipefd[1]);
            
        }
        else{
            close(pipefd[1]);
            dup2(pipefd[0],STDIN_FILENO);
            close(pipefd[0]);
        }
        
        system(cmd);
//       char *argv[] = {cmd,NULL};
        /*if(execvp(cmd,argv)<0)
        {   //用exec族函数执行命令
            printf("exec error\n");
            return NULL;
        }*/
    }
    
    wait(0);                                //等待子进程返回
    
    if(type=='r'){
        close(pipefd[1]);
        return fdopen(pipefd[0],"r");	//由于程序需要返回的参数是文件指针，因此需要用fdopen函数将描述符打开，其返回值为相应的文件指针
    }else{
        close(pipefd[0]);
        return fdopen(pipefd[1],"w");
    }
}

int chmod_excute_mypopen(char *file )
{
	char cmd[SIZE_128] ;
	char popen_r[SIZE_128] ;
	FILE *fp_popen ;
	
	//Add executable ability to l2tp_add.sh and excute it.
	memset(cmd, 0, sizeof(cmd));
	sprintf(cmd, "chmod a+x %s", file);
	printf("After chmod , before system\n");
	system(cmd) ;
	
	/*Use my popen to get whether there is error info 
	* coming from the fp_popen or not.
	*/
	fp_popen = mypopen(file, 'r');
	memset(popen_r, 0 , sizeof(popen_r)) ;
	fgets(popen_r, sizeof(popen_r), fp_popen);
	
	/*If the message from fp_popen ,length < 1, 
	  It prove the commond excute success .
	*/
//	printf("popen_r:%d\n", strlen(popen_r));
	if(strlen(popen_r) > 0)
	{
		printf("shell execut error \n");
		return 0 ;
	}
	
//	memset(cmd, 0, sizeof(cmd));
//	sprintf(cmd, "rm -rf %s", file);
//	system(cmd) ;
	
	return 1 ;
}

/*Excute the myopen function ,and return 
* the result .
*/
int excute_mypopen(char cmd[])
{
	char popen_result[SIZE_128] ;
	FILE *fp ;

	if(!strcmp(cmd, ""))
	{
		printf("cmd is NULL ,return \n");
		return 0 ;
	}
	
	fp = mypopen(cmd, 'r');
	memset(popen_result, 0 , sizeof(popen_result)) ;
	fgets(popen_result, sizeof(popen_result), fp);
	
	if (strcmp(popen_result, "")){
		printf("excute mypopen error\n");
		return 0 ;
	}
	
	return 1 ;
}

void excute_delbr(int br_flag)
{
	char cmd[SIZE_64] ;

	memset(cmd, 0, sizeof(cmd));
	sprintf(cmd, "brctl delbr br%d", br_flag);
	system(cmd);
}




